//
//  XJOrderDetailBottomView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/28.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJOrderDetailBottomView.h"

#import "XJMyOrderBtnModel.h" //按钮模型


@interface XJOrderDetailBottomView()
@property(nonatomic, strong) UIButton *firstButton;
@property(nonatomic, strong) UIButton *secondButton;

/**记录模型数组*/
@property (nonatomic, strong) NSMutableArray *buttonModelArray;

@end
@implementation XJOrderDetailBottomView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/




#pragma mark - ====
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (UIButton *)firstButton {
    if (!_firstButton) {
        _firstButton = [UIButton buttonWithFrame:CGRectZero target:self action:@selector(clickFirstButtonAction) bgc:nil title:@"确认收货"];
    }
    return _firstButton;
}

- (UIButton *)secondButton {
    if (!_secondButton) {
        _secondButton = [UIButton buttonWithFrame:CGRectZero target:self action:@selector(clickSecondButtonAction) bgc:nil title:@"申请售后"];
    }
    return _secondButton;
}

- (NSMutableArray *)buttonModelArray {
    if (!_buttonModelArray) {
        _buttonModelArray = [NSMutableArray array];
    }
    return _buttonModelArray;
}

- (void)setupView {
    [self addSubview:self.firstButton];
    
    [self addSubview:self.secondButton];
}




#pragma mark - ====
- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    [self.firstButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(100, 40));
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.secondButton.mas_left).offset(-10);
    }];
    
    [self.secondButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 40));
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-15);
    }];
}


#pragma mark - ==== 赋值
- (void)configureOrderDetailBottomViewHideFirstBtn:(BOOL)hide FistTitle:(NSString *)str1 secondTitle:(NSString *)str2 {
    self.firstButton.hidden = hide;
    [self.firstButton setTitle:str1 forState:(UIControlStateNormal)];
    
    [self.secondButton setTitle:str2 forState:(UIControlStateNormal)];
}


- (void)configureOrderDetailBottomViewWithButtonArray:(NSArray *)btnArr {

    //备份一份数组
    [self.buttonModelArray  addObjectsFromArray:btnArr];
    
    if (btnArr.count == 2) { //两个按钮都显示
        
        self.firstButton.hidden = NO;
        self.secondButton.hidden = NO;

        //数组最多就两个模型 so敢这么写
        XJMyOrderBtnModel *firstBtnM = [btnArr firstObject];
        [self.firstButton setTitle:firstBtnM.text forState:(UIControlStateNormal)];
        
        
        XJMyOrderBtnModel *secondBtnM = [btnArr lastObject];
        [self.secondButton setTitle:secondBtnM.text forState:(UIControlStateNormal)];

    }else if (btnArr.count ==1) { //只显示最后一个按钮
        self.firstButton.hidden = YES;
        self.secondButton.hidden = NO;

        XJMyOrderBtnModel *lastBtnM = [btnArr lastObject];
        [self.secondButton setTitle:lastBtnM.text forState:(UIControlStateNormal)];
        
    }else { //两个都隐藏。
        self.firstButton.hidden = YES;
        self.secondButton.hidden = YES;
    }
}

#pragma mark - ====点击事件
- (void)clickFirstButtonAction {
    if (self.buttonModelArray.count == 2) {
        XJMyOrderBtnModel *btnM = [self.buttonModelArray firstObject];
        
        if (self.firstButtonBlock) {
            self.firstButtonBlock(btnM);
        }
    }
}

- (void)clickSecondButtonAction{
    
    if (self.buttonModelArray.count){
        
        XJMyOrderBtnModel *seondBtnM = [self.buttonModelArray lastObject];
        
        if (self.secondButtonBlock) {
            self.secondButtonBlock(seondBtnM);
        }
    }
}
@end
